/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function initUM() {
  loadHeaderSub( lang );
  loadUserList();
  loadMessages(lang);
  document.title = 'SEPIA: '+msg( lang, 'msg_um' );
}

function loadUserList() {
  var list = $("userList");
  
  try {
    var param = eval('$H({"action":"GetUsers" })').toQueryString();
    var json = sync(param);
    initSelect(list);
    for (var i = 0; i < json.users.length; i++) {
      var opt = document.createElement("option");
      opt.innerHTML = json.users[i].userName;
      opt.value = json.users[i].userName;
      list.appendChild(opt);
    }
  } catch (err) {
    var opt = document.createElement("option");
    opt.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;*No User has been created yet*&nbsp;&nbsp;&nbsp;&nbsp;";
    opt.selected = true;
    list.appendChild(opt);
    opt.disabled = true;
  }
}

function loadUser( userName ) {
  if ( userName=="" ) {
    jQuery('#additional').slideUp("slow", footer);
  } else {
    jQuery('#additional').slideUp("fast", function(){
      var param = eval('$H({"action":"GetUser", "userName": "'+userName+'"})').toQueryString();
      var json = sync(param);
      loadUserIntoForm(json);
      jQuery('#additional').slideDown("slow", footer);
    });
  }
}

function addUser() {
  jQuery('#additional').slideUp("fast", function(){
    var newUserName = $('newUser').value;
    var param = [];
    param.action = "AddUser";
    param.userId = -1;
    param.userName = es( newUserName );
    param.password = "";
    param.description = "";
    param.role = "";
    
    var json = sync(param);
    loadUserIntoForm(json);
    
    var u = json.users[0];
      
    // update userList 
    var opt = document.createElement("option");
    opt.value = newUserName;
    opt.innerHTML = newUserName;
    opt.id = "userList"+u.userName;
    
    $('userList').appendChild( opt );
    $(opt.id).selected = true;
    $('newUser').value = '';
    
    showMessage( msg_suc_updated );
    log( "ADD USER", newUserName );
    jQuery('#additional').slideDown("slow", footer);
  });
  
  return true;
}

function deleteUser() {
  if ( !deleteConfirmed() ) return;
  if ( $('userId').value == 'admin' ) {
    alert('ERROR: Cannot delete the admin account.');
    return;
  }

  var param = eval('$H({"action":"DeleteUser", "userId": "'+$('userId').value+'" })').toQueryString();
  sync(param);
  var json = eval("({'users':[{'userId': -1, 'userName': '', 'password': '', 'role': '', 'description': '' }] })");
  jQuery('#additional').slideUp("slow",footer);
  loadUserIntoForm(json);
  loadUserList();
  log("DELETE USER", $('userName').value);
  showMessage( msg_suc_updated );
}

function saveUser() {
  var param = [];
  param.action      = "UpdateUser";
  param.userId      = $('userId').value;
  param.userName    = es( $('userName').value );
  if ($('password').value) {
    param.password    = es( $('password').value );
  }
  param.role        = es( $('role').value );
  param.description = es( $('description').value );
  sync(param);
  loadUserList();
  log("UPDATE USER",$('userName').value);
  
  jQuery("#userList option[value='"+$('userName').value+"']").selected(true);
//  var opts = $('taskList').options;
//  for ( var i=0; i<opts.length; i++) {
//    if( opts[i].value==$('userName').value ){
//    opts[i].selected = true;
//    }
//  }
  showMessage( msg_suc_updated );
}

function loadUserIntoForm(json) {
  var u = json.users[0];
  $('newUser').value = '';
  
  $('userId').value = u.userId;
  $('userName').value = ues( u.userName );
  $('password').value = '';//ues( u.password )
  $('role').value = ues( u.role );
  $('description').value = ues( u.description );
  
  jQuery("#role option[value='"+$('role').value+"']").selected(true);
}

jQuery('#role').load(function() {
  // not working?
  jQuery('#role option[value='+userRole+']').selected(true);
});